{% extends 'base.html' %}
{% block title %}Nodes{% endblock %}
{% block nav-item-1 %}layui-nav-itemed{% endblock %}
{% block nav-child-1-1 %}layui-this{% endblock %}
{% block content %}
    {% csrf_token %}
<span class="layui-breadcrumb">
  <a href="#">首页</a>
  <a href="#">Kubernetes</a>
  <a><cite>Nodes</cite></a>
</span>
<hr class="layui-border-red">
<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-row layui-col-space10">
            <div class="layui-col-md12">
{#                <button class="layui-btn" style="float: left">创建</button>#}
                <button class="layui-btn" style="float: left" id="searchBtn">搜索</button>
                <input type="text" name="name" class="layui-input" style="width: 150px;float: left;margin-left: 5px">
            </div>

            <div class="layui-col-md12">
                <table id="test" lay-filter="test"></table>
                <script type="text/html" id="barDemo">
                  <a class="layui-btn layui-btn-xs" lay-event="details">详情</a>
                  <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="yaml">YAML</a>
{#                  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>#}
                </script>
            </div>
        </div>
    </div>
</div>
{% endblock %}
{% block js %}
<script>
layui.use('table', function(){
  var table = layui.table;
  var $ = layui.jquery;
  //动态渲染表格
  table.render({
    elem: '#test'
    ,url: '{% url 'node_api' %}' //数据接口
    ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
    ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
      title: '提示'
      ,layEvent: 'LAYTABLE_TIPS'
      ,icon: 'layui-icon-tips'
    }]
    ,page: true //开启分页
    ,cols: [[ //表头
      {field: 'name', title: '名称', sort: true}
      ,{field: 'labels', title: '标签',width: 200,templet: labelsFormat}
      ,{field: 'status', title: '准备就绪'}
      ,{field: 'scheduler', title: '可调度'}
      ,{field: 'cpu', title: 'CPU'}
      ,{field: 'memory', title: '内存'}
      ,{field: 'kebelet_version', title: 'kubelet版本'}
      ,{field: 'cri_version', title: 'CRI版本'}
      ,{field: 'create_time', title: '创建时间'}
      ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
    ]]
      ,id: "TT"
  });
  // 标签格式化
    function labelsFormat(d) {
        result = "";
        if (d.labels == null) {
            return "None"
        } else {
            for(let key in d.labels) {
                result += '<span style="border: 1px solid #009688;border-radius: 5px">' + key + ':' + d.labels[key] + '</span><br>'
            }
            console.log(result);
            return result
        }
    }
  //监听行工具事件
  table.on('tool(test)', function(obj){
    var data = obj.data;
    //console.log(obj)
    if(obj.event === 'del'){
        // 暂时关闭
    } else if(obj.event === 'yaml'){
        // 查看YAML
        layer.open({
            title: "YAML",
            type: 2,  //从一个网址加载
            area: ["50%", "60%"],
            content: '{% url 'ace_editor' %}?resource=node&' + 'namespace' + '&name=' + data.name
        })
    } else if(obj.event == 'details') {
        window.location.href="{% url 'node_details' %}?node_name=" + data.name
    }
  });
    // 监控搜索事件
    $('#searchBtn').click(function(){
        var search_key = $('input[name="name"]').val();
        table.reload('TT', {
          where: {
              search_key: search_key
          }
        });
    })

});
</script>
{% endblock %}